/**
 * ESUI (Enterprise Simple UI)
 * Copyright 2015 Baidu Inc. All rights reserved.
 *
 * @ignore
 * @file image、flash列表，可选中、下载图标、删除图标
 * @author hongfeng (homfen@outlook.com)
 */

define(
    function (require) {

        var u = require('underscore');
        var $ = require('jquery');
        var esui = require('esui');
        var eoo = require('eoo');
        var etpl = require('etpl');
        var painters = require('esui/painters');
        var InputControl = require('esui/InputControl');

        var ImagePanel = eoo.create(
            InputControl,
            {

                /**
                 * 控件类型，始终为 `ImagePanel`
                 *
                 * @type {string}
                 * @readonly
                 * @override
                 */
                type: 'ImagePanel',

                /**
                 * 初始化参数
                 *
                 * @param {Object} options 参数对象
                 * @override
                 */
                initOptions: function (options) {
                    var properties = {
                    };

                    u.extend(properties, options);
                    this.setProperties(properties);
                },

                /**
                 * 初始化控件结构
                 *
                 * @override
                 */
                initStructure: function () {
                    refresh.call(this);
                },

                /**
                 * 绑定事件
                 *
                 * @override
                 */
                initEvents: function () {
                },

                /**
                 * 重渲染
                 *
                 * @method
                 * @protected
                 * @override
                 */
                repaint: painters.createRepaint(
                    InputControl.prototype.repaint,
                    {
                        name: 'rawValue',
                        paint: function (panel, rawValue) {
                            refresh.call(panel, rawValue);
                        }
                    }
                )
            }
        );

        /**
         * 刷新图标列表
         */
        function refresh(rawValue) {
            if (u.isObject(rawValue) && rawValue.url) {
                rawValue = rawValue.url;
            }
            if (u.isString(rawValue) && /^https?:\/\//.test(rawValue)) {
                this.main.innerHTML = '<img src="' + rawValue + '" />';
            }
            else {
                this.rawValue = '';
            }
        }

        esui.register(ImagePanel);

        return ImagePanel;
    }
);
